/*
 * @(#)XflPackage.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.lang;

import java.io.*;
import java.util.Vector;

/**
 * Clase que almacena los paquetes de funciones disponibles
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XflPackageBank implements FilenameFilter {
	
	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Lista de paquetes de funciones
	 */
	private XflPackage[] list;
	
	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XflPackageBank() {
		String path = System.getProperty("user.dir");
		File pkgdir = new File(path+"/pkg");
		if(!pkgdir.exists() || !pkgdir.isDirectory()) {
			this.list = new XflPackage[0];
			return;
		}
		File[] pkg = pkgdir.listFiles(this);
		
		this.list = new XflPackage[pkg.length];
		for(int i=0; i<list.length; i++) {
			list[i] = new XflPackage(pkg[i]);
		}
	}
	
 	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Estudia si el conjunto de paquetes contiene una definici�n dada
	 */
	public boolean contains(String pkg, String func, int kind) {
		XflPackage xflpkg = getXflPackage(pkg);
		if(xflpkg == null) return false;
		return xflpkg.contains(func,kind);
	}

	/**
	 * Obtiene una instancia de una definici�n
	 */
	public Object instantiate(String pkg, String func, int kind) {
		XflPackage xflpkg = getXflPackage(pkg);
		return xflpkg.instantiate(func,kind);
	}

	/**
	 * Acepta los ficheros que sean directorios
	 * Desarrolla la interfaz FilenameFilter
	 * @param file
	 * @return
	 */
	public boolean accept(File file, String filename) {
		if(file.isDirectory()) return true;
		return false;
	}

	/**
	 * Obtiene la lista de funciones disponibles de un cierto tipo
	 * @param kind
	 * @return
	 */
	public Vector<String> getFunctionNames(int kind) {
		Vector<String> vv = new Vector<String>();
		for(int i=0; i<list.length; i++) {
			String pkgname = list[i].toString();
			String[] funcname = list[i].get(kind);
			for(int j=0; j<funcname.length; j++) vv.addElement(pkgname+"."+funcname[j]);
		}
		return vv;
	}
	
 	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Busca un paquete a partir de su nombre
	 */
	private XflPackage getXflPackage(String pkg) {
		for(int i=0; i<list.length; i++) {
			if(list[i].equals(pkg)) return list[i];
		}
		return null;
	}

}

